package tools.blast;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;

public class blastAnalyzer {

	/**
	 * @param args
	 */
	public static void main(String[] args)throws Exception{
		//*
		blastAnalyzer app=new blastAnalyzer();
		app.sort_beheadBlastResult("/local/membrane_protein/families/SLC/putatively_new_SLC_all_blast.csv","/local/membrane_protein/families/SLC/putatively_new_SLC_all_blast",1,10);
		//*/
		/*
		blastAnalyzer app=new blastAnalyzer();
		app.topTargets("/local/membrane_protein/blast/human.protein_VS_cellular_component.blast","/local/membrane_protein/blast/human.protein_VS_cellular_component",1E-6);
		//*/
		
		
		System.out.println("Done!");
	}
	public blastAnalyzer(){
		
	}
	public void topTargets(String inFile,String outFilePrefix,double maxEvalue)throws Exception{
		blastM8Parser in=new blastM8Parser(new BufferedReader(new FileReader(inFile)));
		BufferedWriter out=new BufferedWriter(new FileWriter(outFilePrefix+"_top.csv"));
		BufferedWriter clusterHigh=new BufferedWriter(new FileWriter(outFilePrefix+"_clusterList_highE.csv"));
		BufferedWriter clusterLow=new BufferedWriter(new FileWriter(outFilePrefix+"_clusterList_lowE.csv"));
		Hashtable clusterH=new Hashtable();
		Hashtable clusterL=new Hashtable();
		if(in.hasMore()){
			blastM8Alignment top;
			blastM8Alignment tmp=in.nextHit();
			top=tmp;
			String name=tmp.qname;
			for(int i=0;in.hasMore();i++){
				if(i%10000==0)
					System.out.println(i);
				tmp=in.nextHit();
				if(name.equals(tmp.qname)){
					if(tmp.score>top.score)
							top=tmp;
				}else{
					out.write(name+"\t"+top.tname+"\t"+top.e_value+"\t"+top.score+"\n");
					String cl=top.tname.split("_")[0];
					if(!cl.equals("0")){
						if(top.e_value<maxEvalue){
							if(clusterH.containsKey(cl))
								clusterH.put(cl,((String)clusterH.get(cl))+","+name);
							else
								clusterH.put(cl,name);
						} else{
							if(clusterL.containsKey(cl))
								clusterL.put(cl,((String)clusterL.get(cl))+","+name);
							else
								clusterL.put(cl,name);
						}
					}
					name=tmp.qname;
					top=tmp;
				}
			}
		}
		out.close();
		for(Enumeration i=clusterH.keys();i.hasMoreElements();){
			String cl=(String)i.nextElement();
			clusterHigh.write(cl+"\t"+clusterH.get(cl)+"\n");
		}
		for(Enumeration i=clusterL.keys();i.hasMoreElements();){
			String cl=(String)i.nextElement();
			clusterLow.write(cl+"\t"+clusterL.get(cl)+"\n");
		}
		clusterHigh.close();
		clusterLow.close();
	}
	public void sort_beheadBlastResult(String inFile,String outPrefix, int nrOfHits,double maxEvalue)throws Exception{
		blastM8Parser in=new blastM8Parser(new BufferedReader(new FileReader(inFile)));
		BufferedWriter out=new BufferedWriter(new FileWriter(outPrefix+"_sorted.csv"));
		BufferedWriter stat=new BufferedWriter(new FileWriter(outPrefix+"_stat.csv"));
		if(in.hasMore()){
			Vector top=new Vector(nrOfHits,1);
			blastM8Alignment tmp=in.nextHit();
			top.add(tmp);
			String name=tmp.qname;
			for(int i=0;in.hasMore();i++){
				if(i%10000==0)
					System.out.println(i);
				tmp=in.nextHit();
				if(name.equals(tmp.qname)){
					int j=0;
					for(;j<top.size()&&j<nrOfHits;j++){
						if(tmp.e_value>maxEvalue){
							j=nrOfHits+2;
							break;
						}else if(tmp.score>((blastM8Alignment)top.get(j)).score)
							break;
					}
					if(j<nrOfHits){
						top.insertElementAt(tmp,j);
						if(top.size()>nrOfHits)
							top.removeElementAt(nrOfHits);
					}
				}else{
					stat.write(name);
					for(int j=0;j<top.size();j++){
						blastM8Alignment t=(blastM8Alignment)top.get(j);
						stat.write("\t"+t.tname.split("_")[0]);
						out.write(t.toString()+"\n");
					}
					stat.write("\n");
					top=new Vector(nrOfHits,1);
					top.add(tmp);
					name=tmp.qname;
				}
			}
		}
		out.close();
		stat.close();
	}
}
